home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Piskvorky / source / pbrain / PISKTUR.CPP < prev    next >
C/C++ Source or Header  |  2006-04-12  |  4KB  |  210 lines

  1. /*
  2.   (C) 2004-2006  Petr Lastovicka
  3.  
  4.   contents of this file are subject to the Reciprocal Public License ("RPL")
  5. */
  6. #include <windows.h>
  7. #pragma hdrstop
  8. #include <assert.h>
  9. #include <stdio.h>
  10. #include "board.h"
  11.  
  12. #define MATCH_SPARE 7      //how much is time spared for the rest of game
  13. #define TIMEOUT_PREVENT 3  //how much is alfabeta slower when the depth is increased
  14.  
  15. const char *infotext="author=\"Petr Lastovicka\", version=\"7.5\", country=\"Czech Republic\",  www=\"http://web.quick.cz/lastp\"";
  16.  
  17.  
  18. void brain_init() 
  19. {
  20.   if(width<5 || width>127 || height<5 || height>127){
  21.     pipeOut("ERROR size of the board");
  22.     return;
  23.   }
  24.   init();
  25.   pipeOut("OK");
  26. }
  27.  
  28. void brain_restart()
  29. {
  30.   brain_init();
  31. }
  32.  
  33. int brain_takeback(int x,int y)
  34. {
  35.   Psquare p=Square(x,y);
  36.   if(p<boardb || p>=boardk || !p->z) return 2;
  37.   p->z=0;
  38.   moves--;
  39.   evaluate(p);
  40.   lastMove=0;
  41.   return 0;
  42. }
  43.  
  44. static bool doMove0(Psquare p, int z)
  45. {
  46.   if(p<boardb || p>=boardk || p->z) return false;
  47.   p->z= z;
  48.   moves++;
  49.   evaluate(p);
  50.   lastMove=p;
  51.   return true;
  52. }
  53.  
  54. void brain_my(int x,int y)
  55. {
  56.   if(!doMove0(Square(x,y),1)){
  57.     pipeOut("ERROR my move [%d,%d]",x,y);
  58.   }
  59. }
  60.  
  61. void brain_opponents(int x,int y) 
  62. {
  63.   if(!doMove0(Square(x,y),2)){
  64.     pipeOut("ERROR opponents's move [%d,%d]",x,y);
  65.   }
  66. }
  67.  
  68. void brain_block(int x,int y)
  69. {
  70.   if(!doMove0(Square(x,y),3)){
  71.     pipeOut("ERROR winning move [%d,%d]",x,y);
  72.   }
  73. }
  74.  
  75. bool doMove(Psquare p)
  76.   if(p<boardb || p>=boardk || p->z) return false;
  77.   if(!terminate || !resultMove) resultMove=p;
  78.   return true; 
  79. }
  80.  
  81. void computer()
  82. {
  83.   int i;
  84.   DWORD t0,t1;
  85.   Psquare p;
  86.   
  87.   resultMove=0;
  88.   holdMove=0;
  89.   highestEval=0;
  90.   firstMove();  
  91.   if(resultMove) return;
  92.  
  93.   for(p=boardb; p<boardk; p++){
  94.     p->inWinMoves= winMoves1+MwinMoves;
  95.   }
  96.   UwinMoves=winMoves1;
  97.  
  98.   if(doMove(try4(0))){
  99.     #ifdef DEBUG
  100.       print("win4");
  101.     #endif
  102.     static bool w=false;
  103.     if(!w && resultMove->h[0].pv<H31){
  104.       w=true;
  105.       pipeOut("MESSAGE  I win !");
  106.     }
  107.     return; 
  108.   }
  109.   attackDone=defendDone=defendDone1=testDone=carefulAttack=carefulDefend=false;
  110.   loss4=try4(1);
  111.   if(loss4) doMove(loss4);
  112.   else getBestEval();
  113.   assert(resultMove);
  114.   #ifdef DEBUG
  115.     resulty=0;
  116.     mt=0;
  117.   #endif
  118.  
  119.   for(i=4; i<=50; i+=2){
  120.     depth=i;
  121.     #ifdef DEBUG
  122.       printXY(200,0,80,"   depth= %d   ", depth);
  123.     #endif
  124.     benchmark=0;
  125.     t0=GetTickCount();
  126.     computer1();
  127.     t1=GetTickCount();
  128.     if(terminate || t1+TIMEOUT_PREVENT*(t1-t0)>=stopTime()) break;
  129.   }
  130.  
  131.   if(terminate) depth-=2;
  132.   pipeOut("DEBUG depth %d, nodes %d", depth, benchmark);
  133.   #ifdef DEBUG
  134.     if(resulty>0) print("win");
  135.     if(resulty<0) print("loss");
  136.     if(resulty==0) print("---");
  137.     printXY(60,18,130,"moves=%d",benchmark);
  138.   #endif
  139. }
  140.  
  141. DWORD stopTime()
  142. {
  143.   return start_time + min(info_timeout_turn, info_time_left/MATCH_SPARE)-30;
  144. }
  145.  
  146. void brain_turn() 
  147. {
  148.   computer();
  149.   do_mymove(resultMove->x, resultMove->y);
  150. }
  151.  
  152. void brain_end()
  153. {
  154. }
  155.  
  156. //----------------------------------------------------------------------
  157. #ifdef DEBUG
  158.  
  159. HWND wnd=FindWindow("Piskvork",0);
  160.  
  161. void vprint(int x, int y, int w, char *format, va_list va)
  162. {
  163.   HDC dc= GetDC(wnd);
  164.   RECT rc;
  165.   rc.top=y;
  166.   rc.bottom=y+16;
  167.   rc.left= x - w/2;
  168.   rc.right= rc.left + w;
  169.   char buf[128];
  170.   int n = vsprintf(buf,format,va);
  171.   SetTextAlign(dc,TA_CENTER);
  172.   ExtTextOut(dc, x,y, ETO_OPAQUE, &rc, buf,n, 0);
  173.   ReleaseDC(wnd,dc);
  174. }
  175.  
  176. void printXY(int x, int y, int w, char *format, ...)
  177. {
  178.   va_list va;
  179.   va_start(va,format);
  180.   vprint(x,y,w,format,va);
  181.   va_end(va);
  182. }
  183.  
  184. void print(char *format, ...)
  185. {
  186.   RECT rc;
  187.   GetClientRect(wnd,&rc);
  188.   va_list va;
  189.   va_start(va,format);
  190.   vprint(rc.right*900/1000, 0, 80, format,va);
  191.   va_end(va);
  192. }
  193.  
  194. void paintSquare(Psquare p)
  195. {
  196.   SendMessage(wnd,WM_APP+123, p->z, MAKELPARAM(p->x,p->y));
  197. }
  198.  
  199. void brain_eval(int x,int y)
  200. {
  201.   Psquare p= Square(x,y);
  202.   printXY(300,0,60,"%d,%d", p->h[0].pv, p->h[1].pv);
  203.   printXY(300,16,60,"%d,%d", getEval(0,p),getEval(1,p));
  204.   printXY(300,32,60,"%d", getEval(p));
  205. }
  206.  
  207.  
  208. #endif
  209.